import Vue from 'vue'
import Router from 'vue-router'
import bookManager from '@/components/bookManager'
import login from '@/components/login'
import store from '@/store/index'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: login
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/bookManager',
      name: 'bookManager',
      component: bookManager
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === "/") {
    next()
  } else {
    //从store中获取用户登录信息
    let isLogin = store.state.userStore.isLogin
    if (!isLogin) {
      //提示未登录
      Vue.prototype.$message.error("无权访问,请先登录?")
      //未登录进行拦截，跳转到登录页面
      next("/")
    } else {
      next()
    }
  }
})
export default router
